<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\DirectoryService;
use App\Http\Requests\UserBasicInfoSearchRequest;
use Illuminate\Support\Facades\Log;

class DirectoryController extends Controller
{
    protected $directoryService;

    public function __construct(DirectoryService $directoryService) 
    {
        $this->directoryService = $directoryService;
    }


    //For POCOR-8104 Start...
    public function getUserTypeList(Request $request)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getUserTypeList($params);
            return $this->sendSuccessResponse("User Type List Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch User Type List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('User Type List Not Found');
        }
    }


    public function getUniqueOpenemisId(Request $request)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getUniqueOpenemisId($params);
            return $this->sendSuccessResponse("Openemis Id Generated.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to generate Openemis Id.',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Failed to generate Openemis Id.');
        }
    }


    public function getAutoGeneratedPassword(Request $request)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getAutoGeneratedPassword($params);
            
            if(is_array($data)){
                return $this->sendSuccessResponse("Password is generated.", $data);
            }


            if($data == 1){
                return $this->sendErrorResponse('Argument(s) not an integer.');
            } else if($data == 2){
                return $this->sendErrorResponse('Argument(s) not an integer.');
            } else if($data == 3){
                return $this->sendErrorResponse('Number of password capitals required exceeds password length.');
            } else if($data == 4){
                return $this->sendErrorResponse('Number of password numerals exceeds password length.');
            } else if($data == 5){
                return $this->sendErrorResponse('Number of password capitals exceeds password length.');
            } else if($data == 6){
                return $this->sendErrorResponse('Number of password special characters exceeds specified password length.');
            }
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to generate Password.',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Failed to generate Password.');
        }
    }



    public function getContactTypes(Request $request)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getContactTypes($params);
            return $this->sendSuccessResponse("Contact Type List Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Contact Type List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Contact Type List Not Found');
        }
    }



    public function getStaffCustomFields(Request $request)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getStaffCustomFields($params);
            return $this->sendSuccessResponse("Staff Custom Fields List Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Staff Custom Fields List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Staff Custom Fields List Not Found');
        }
    }


    public function getFieldOptions(Request $request)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getFieldOptions($params);
            return $this->sendSuccessResponse("Field Options List Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Field Options List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Field Options List Not Found');
        }
    }


    public function getFieldOptionData(Request $request, $fieldOptionId)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getFieldOptionData($params, $fieldOptionId);
            return $this->sendSuccessResponse("Field Option Data Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Field Option Data from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Field Option Data Not Found');
        }
    }



    public function getUserByIdentityNumber(Request $request, $identityTypeId, $identityNumber)
    {
        try {
            $params = $request->all();
            $data = $this->directoryService->getUserByIdentityNumber($params, $identityTypeId, $identityNumber);

            if(!empty($data) && $data['user_exist'] == 1){
                return $this->sendSuccessResponse("User already exists with this identity type & identity number.", $data);
            } else {
                return $this->sendSuccessResponse("User not found.", $data);
            }
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch User Data from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('User Data Not Found');
        }
    }


    public function getUserByBasicInfo(UserBasicInfoSearchRequest $request)
    {
        try {
            $params = $request->all();
            
            $data = $this->directoryService->getUserByBasicInfo($params);

            return $this->sendSuccessResponse("User Data Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch User Data from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('User Data Not Found');
        }
    }


    public function getRelationshipTypes(Request $request)
    {
        try {
            $params = $request->all();
            
            $data = $this->directoryService->getRelationshipTypes($params);

            return $this->sendSuccessResponse("Relationship Types Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Relationship Types from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Relationship Types Not Found');
        }
    }


    public function getStaffType(Request $request)
    {
        try {
            $params = $request->all();
            
            $data = $this->directoryService->getStaffType($params);

            return $this->sendSuccessResponse("Staff Types Found.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Staff Types from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Staff Types Not Found');
        }
    }

    //For POCOR-8104 End...


    //For POCOR-8194 Start...
    public function getStaffPositionGrades(Request $request)
    {
        try {
            $params = $request->all();
            
            $data = $this->directoryService->getStaffPositionGrades($params);

            return $this->sendSuccessResponse("Successful.", $data);
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Staff position grades.',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse($e->getMessage());
        }
    }
    //For POCOR-8194 End...
}
